package com.controller;


import com.bean.Enterprise;
import com.bean.OrderItem;
import com.bean.User;
import com.service.OrderItemService;
import com.service.impl.OrderItemServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

public class OrderItemServlet extends BaseServlet {

    private OrderItemService orderItemService=new OrderItemServiceImpl();

    /**
     * 用户评价时查看相应的商品项
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getOrderItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String  fid=req.getParameter("id");
        Integer id=Integer.parseInt(fid);
        OrderItem orderItem=orderItemService.getOrderItem(id);
        req.setAttribute("orderItem",orderItem);
        req.getRequestDispatcher("/page/user/evaluate.jsp").forward(req,resp);
    }

    /**
     *商家 发货
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void sendOrderItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //String enterpriseid=req.getParameter("enterpriseid");
        String oid=req.getParameter("id");
        Integer id=Integer.parseInt(oid);
        try {
            orderItemService.sendOrderItem(id);
            System.out.println("已发货！");
        } catch (Exception e) {
            e.printStackTrace();
        }
        req.getRequestDispatcher("/page/enterprise/indexEnterprise.jsp").forward(req,resp);
    }

    /**
     * 企业订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void enterpriseOrderItems(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //String enterpriseid=req.getParameter("id");
        Enterprise loginEnterprise= (Enterprise) req.getSession().getAttribute("enterprise");
        if(loginEnterprise==null){
            req.getRequestDispatcher("/page/loginEnterprise/login.jsp").forward(req,resp);
            return;
        }
        List<OrderItem> myorderItems = orderItemService.showMyOrderItems(loginEnterprise.getEnterpriseid());
        req.setAttribute("myorderItems",myorderItems);
        req.getRequestDispatcher("/page/enterprise/myOrderItems.jsp").forward(req,resp);
    }

    /**
     * 企业查看相应状态订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void enterpriseOrderItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       //String enterpriseid=req.getParameter("id");
       String ostatus=req.getParameter("status");
       Integer status=Integer.parseInt(ostatus);
        Enterprise loginEnterprise= (Enterprise) req.getSession().getAttribute("enterprise");
        if(loginEnterprise==null){
            req.getRequestDispatcher("/page/loginEnterprise/login.jsp").forward(req,resp);
            return;
        }
        String enterpriseid=loginEnterprise.getEnterpriseid();
        //List<OrderItem> myorderItems = orderItemService.showOrderItemByStatus(status);
        List<Map<String,Object>>myorderItems=orderItemService.queryStatus(status,enterpriseid);
        req.setAttribute("myorderItems",myorderItems);
        System.out.println(myorderItems);
        req.getRequestDispatcher("/page/enterprise/myOrderItems.jsp").forward(req,resp);
    }

    /**
     * 用户签收商品
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void receiverOrderItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String orderitemid=req.getParameter("orderid");
        Integer id=Integer.parseInt(orderitemid);
        User loginUser=(User)req.getSession().getAttribute("user");
        System.out.println(loginUser.getUserid());
        if(loginUser==null){
            req.getRequestDispatcher("/page/loginUser/login.jsp").forward(req,resp);
            return;
        }
        orderItemService.receiverOrderItem(id);
        req.getRequestDispatcher("orderServlet?action=showMyOrders&userid="+loginUser.getUserid()).forward(req,resp);
    }

    /**
     * 用户查看状态订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void userOrderItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String s=req.getParameter("status");
        Integer status=Integer.parseInt(s);
        List<Map<String,Object>> orderitems=null;
        User user= (User) req.getSession().getAttribute("user");
        String userid=user.getUserid();
        orderitems=orderItemService.querylist(userid,status);
        req.setAttribute("orderitems",orderitems);
        System.out.println(orderitems);
        req.getRequestDispatcher("/page/user/orderitem.jsp").forward(req,resp);
    }
}
